import Ember from 'ember';
import Changeset from 'ember-changeset';
import NursegroupValidations from '../../../validations/nurseGroup';
import lookupValidator from 'ember-changeset-validations';

export default Ember.Controller.extend(NursegroupValidations,{
  nurseGroupModel:Ember.computed('nurseGroupInfo',function(){
    var model = this.get("nurseGroupInfo");
    if (!model) {
        return null;
    }
    return new Changeset(model, lookupValidator(NursegroupValidations), NursegroupValidations);
  }),
  //未选成员列表
  staffNameList:Ember.computed('staffList.@each.hasChoosed',function(){
    var staff=this.get('staffList');
    return staff.filter(function(staff){
      return staff.hasChoosed===false;
    });
  }),
  //已选成员列表
  chooseStaff:Ember.computed('staffList.@each.hasChoosed',function(){
    var staff=this.get('staffList');
    return staff.filter(function(staff){
      return staff.hasChoosed===true;
    });
  }),
  chooseStaffName:Ember.computed('chooseStaff',function(){
    var staff=this.get('chooseStaff');
    staff.filter(function(staff){
      return staff.name;
    });
  }),
  //未选房间列表
  // roomNameList:Ember.computed('roomList.@each.hasChoosed',function(){
  //   var room=this.get('roomList');
  //   return room.filter(function(room){
  //     return room.hasChoosed===false;
  //   });
  // }),
  // //已选房间列表
  // chooseRoom:Ember.computed('roomList.@each.hasChoosed',function(){
  //   var room=this.get('roomList');
  //   return room.filter(function(room){
  //     return room.hasChoosed===true;
  //   });
  // }),
  //组长列表
  leaderList:Ember.computed('chooseStaff',function(){
    var leaderList=this.get('chooseStaff');
    leaderList.forEach(function(leader){
      leader.set('namePinyin',pinyinUtil.getFirstLetter(leader.get("name")));
    });
    return leaderList;
  }),
  //默认组长
  defaultLeader:Ember.computed('nurseGroupInfo.leader','leaderList',function(){

    if(this.get("nurseGroupInfo.leader")&&this.get("nurseGroupInfo.leader").content){
      return this.get("nurseGroupInfo.leader");
    }
    let id=this.get('leaderList').get('firstObject.id');
    var leader=this.store.peekRecord('user',id);
    return leader;
  }),
  actions:{
    invalid() {
        //alert("error");
    },
    //保存护工组
    saveNurseGroup(){
      var nurseGroupInfo=this.get('nurseGroupInfo');
      nurseGroupInfo.set('leader',this.get('defaultLeader'));
      var nurseGroupModel=this.get('nurseGroupModel');
      var editMode=this.get('editMode');
      var _self=this;
      if(editMode=='add'){
        //验证是否通过
        nurseGroupModel.validate().then(function(){
          if(nurseGroupModel.get('errors.length')===0){
            App.lookup('controller:business.mainpage').openPopTip("正在保存");
            nurseGroupModel.save().then(

              function(newNurseGroupData){
                App.lookup('controller:business.mainpage').showPopTip("保存成功");
                //保存已选护工
                var chooseStaff=_self.get('chooseStaff');
                chooseStaff.forEach(function(staff){
                var staffData=  _self.store.peekRecord('user',staff.get('id'));
                    staffData.set('nurseGroup',newNurseGroupData);
                    staffData.set('isGroupFlag',1);
                    staffData.save();
                });
                //保存已选房间
                // var chooseRoom=_self.get('chooseRoom');
                // chooseRoom.forEach(function(room){
                //   var roomData= _self.store.peekRecord('room',room.get('id'));
                //   roomData.set('nurseGroup',newNurseGroupData);
                //   roomData.set('isGroupFlag',1);
                //   roomData.save();
                // });
                let mainpageController = App.lookup('controller:business.mainpage');
                mainpageController.switchMainPage('nursing-worker-group-management');
              }
            );
          }else{
            nurseGroupModel.set("validFlag",Math.random());
            alert("校验失败");
          }
        });
          //增加新的护工组，保存数据
      }else{
        //验证是否通过
          nurseGroupModel.validate().then(function(){
            //修改编辑已有护工组
            nurseGroupModel.save().then(
              function(newNurseGroupData){
                App.lookup('controller:business.mainpage').showPopTip("保存成功");
                //保存未选护工列表
                var staffNameList=_self.get('staffNameList');
                staffNameList.forEach(function(staff){
                  var item=staffNameList.findBy('id',staff.get('id'));
                  item.set('hasChoosed',false);
                  var staffData= _self.store.peekRecord('user',staff.get('id'));
                  staffData.set('nurseGroup',null);
                  staffData.set('isGroupFlag',0);
                  staffData.save();
                });
                //保存已选护工列表
                var chooseStaff=_self.get('chooseStaff');
                chooseStaff.forEach(function(staff){
                  var item=chooseStaff.findBy('id',staff.get('id'));
                  item.set('hasChoosed',true);
                  var staffData=  _self.store.peekRecord('user',staff.get('id'));
                      staffData.set('nurseGroup',newNurseGroupData);
                      staffData.set('isGroupFlag',1);
                      staffData.save();
                });
                //保存未选房屋列表
                // var roomNameList=_self.get('roomNameList');
                // roomNameList.forEach(function(room){
                //   var item=roomNameList.findBy('id',room.get('id'));
                //   item.set('hasChoosed',false);
                //   var roomData=_self.store.peekRecord('room',room.get('id'));
                //   roomData.set('nurseGroup',null);
                //   roomData.set('isGroupFlag',0);
                //   roomData.save();
                // });
                //保存已选房屋列表
                // var chooseRoom=_self.get('chooseRoom');
                // chooseRoom.forEach(function(room){
                //   var item=chooseRoom.findBy('id',room.get('id'));
                //   item.set('hasChoosed',true);
                //   var roomData= _self.store.peekRecord('room',room.get('id'));
                //   roomData.set('nurseGroup',newNurseGroupData);
                //   roomData.set('isGroupFlag',1);
                //   roomData.save();
                // });
                _self.set('detailEdit',false);
                var route=App.lookup('route:business.mainpage.nursing-worker-group-detail');
                App.lookup('controller:business.mainpage').refreshPage(route);//刷新页面
              }
            );
          });

      }


    },
    //编辑按钮
    detailEditClick:function(floor){
      this.set('detailEdit',true);
    },
    //取消按钮
    detailCancel:function(){
      var id=this.get('id');
      var editMode=this.get('editMode');
      if(id&&editMode=='edit'){
        this.set('detailEdit',false);
        this.get('nurseGroupInfo').rollbackAttributes();
        var route=App.lookup('route:business.mainpage.nursing-worker-group-detail');
        App.lookup('controller:business.mainpage').refreshPage(route);
      }else{
        let mainpageController = App.lookup('controller:business.mainpage');
        mainpageController.switchMainPage('nursing-worker-group-management');
      }
    },
    //选择组长
    selectLeader(leader) {
      var leaderStaff=this.store.peekRecord('user',leader.get('id'));
        this.set('nurseGroupInfo.leader',leaderStaff);
    },
    //删除按钮
    delById : function() {
      var _self = this;
      App.lookup('controller:business.mainpage').showConfirm("是否确定删除此护工组记录",function(){
        _self.send('cancelPassSubmit',_self.get('nurseGroupInfo'));
      });
    },
    //弹窗取消
    invitation(){
      this.set('showpopInvitePassModal',false);
    },
    //弹窗确定，删除记录
    cancelPassSubmit(nursegroup){
      var _self=this;
      App.lookup('controller:business.mainpage').openPopTip("正在删除");
      this.set('showpopInvitePassModal',false);
      //将当前护工组的leader清空
      console.log('nursegroup:',nursegroup);
      nursegroup.set("leader",null);
      //将当前护工组设为删除状态
      nursegroup.set("delStatus", 1);
      //保存护工组状态
      nursegroup.save().then(function() {
          //将当前护工组下组员的状态设为未选
          var chooseStaff=_self.get('chooseStaff');
          chooseStaff.forEach(function(staff){
            var staffData= _self.store.peekRecord('user',staff.get('id'));
            staffData.set('nurseGroup',null);
            staffData.set('isGroupFlag',0);
            staffData.save();
          });
          //将当前护工组下房间的状态设为未选
          // var chooseRoom = _self.get('chooseRoom');
          // chooseRoom.forEach(function(room){
          //   var roomData = _self.store.peekRecord('room',room.get('id'));
          //   roomData.set('nurseGroup',null);
          //   roomData.set('isGroupFlag',0);
          //   roomData.save();
          // });
          App.lookup('controller:business.mainpage').showPopTip("删除成功");
          var mainpageController = App.lookup('controller:business.mainpage');
          mainpageController.switchMainPage('nursing-worker-group-management');
      });
		},
    //选中护工
    changeStaffType(itemCode){
      let item = this.get('staffNameList').findBy('id',itemCode);
      item.set('hasChoosed',true);
    },
    //取消选中护工
    changeStaffBack(itemCode){
      var item = this.get('chooseStaff').findBy('id',itemCode);
      item.set('hasChoosed',false);
    },
    // //选中房间
    // changeRoomType(itemCode){
    //   var item = this.get('roomNameList').findBy('id',itemCode);
    //   item.set('hasChoosed',true);
    // },
    // //取消选中房间
    // changeRoomBack(itemCode){
    //   var item =this.get('chooseRoom').findBy('id',itemCode);
    //   item.set('hasChoosed',false);
    // }
  }
});
